import {showModal, showToast} from "../../utils/asyncWx"
import {request} from "../../request/index"
Page({

  data: {
    address:{},
    cart: [],
    totalPrice: 0,
    totalNum: 0
  },

  onShow() {
    let address = wx.getStorageSync("address");
    //获取缓存中购物车数据
    let cart = wx.getStorageSync("cart") || [];
    //过滤后的购物车数组 checked=true的物品
    cart = cart.filter(v => v.checked)
    this.setData({address})

    wx.setStorageSync('token', 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo')
   
    //总数量 总价格
    let totalPrice = 0
    let totalNum = 0
    cart.forEach(v => {
        totalPrice += v.num * v.goods_price
        totalNum += v.num
      }
      
    )

    this.setData({
      cart,
      totalPrice,
      totalNum,
      address
    })
  },

  async handlegateTo() {

    let newCart = wx.getStorageSync("cart");
    newCart = newCart.filter(v=>!v.checked)
    wx.setStorageSync("cart", newCart);
     wx.switchTab({
          url: '/pages/cart/index',
      });

   
     wx.showToast({
      title: '你觉得我卖得起吗 ヾ(•ω•`)o',
      icon:"none",
    });
  }


})